package main

import (
	"fmt"
	"github.com/360EntSecGroup-Skylar/excelize"
)

//导出到Excel
func writeExcel() {
	xlsx := excelize.NewFile()
	// Create a new sheet.
	index := xlsx.NewSheet("Sheet2")
	// Set value of a cell.
	xlsx.SetCellValue("Sheet2", "A2", "Hello world.")
	xlsx.SetCellValue("Sheet1", "B1", 100)
	xlsx.SetCellValue("Sheet1", "B2", 200)
	xlsx.SetCellValue("Sheet1", "B3", 300)
	xlsx.SetCellValue("Sheet1", "B4", 400)
	// Set active sheet of the workbook.
	xlsx.SetActiveSheet(index)
	// Save xlsx file by the given path.
	err := xlsx.SaveAs("E:/Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
	}
}

//读取Excel
func readExcel() {
	xlsx, err := excelize.OpenFile("E:/Workbook.xlsx")
	if err != nil {
		fmt.Println(err)
		return
	}
	// Get value from cell by given worksheet name and axis.
	cell := xlsx.GetCellValue("Sheet1", "B2")
	fmt.Println(cell)
	// Get all the rows in the Sheet1.
	rows := xlsx.GetRows("Sheet1")
	for _, row := range rows {
		for _, colCell := range row {
			fmt.Print(colCell, "\t")
		}
		fmt.Println()
	}
}
func main() {
	writeExcel()
	readExcel()
}
